Inner Queries
caution
This feature is available starting from QQL 5.6.78.
With inner queries, you can embed a select
statement within the from
clause of another select
statement. Queries can be combined in a sequential manner, facilitating layered data analysis.
Inner queries allow for breaking down complex queries into smaller components where each subquery performs a distinct task.
Limitations
- Scope: Inner subqueries can only be used in the
from
clause. - Performance: Be aware that using multiple subqueries may impact performance.
Use Cases
1. Data resampling before final calculations
This query gets the latest value in each millisecond and calculates the sum for each second.
select sum{}(v) from (
select value as v from stream
over time (1ms)
)
over time (1s)
2. Alternative to having
clause
In this case, we use the inner query because the min > 27020
condition can affect the result set.
SELECT * FROM (
SELECT min{}(entries[this is TradeEntry][0].price) as 'min'
FROM "COINBASE"
OVER TIME(1s)
WHERE symbol == 'BTC/USDT'
) WHERE min > 27020
3. More intuitive and performant ways of making queries
This query counts the distinct number of symbols.
Without an inner query, the COLLECT_UNIQUE
function accumulates symbols in an array that can require large memory allocations:
SELECT SIZE(COLLECT_UNIQUE{}(symbol)) FROM "securities"
With an inner query:
SELECT COUNT{}() FROM (SELECT DISTINCT symbol FROM "securities")